package com.bonc.ioc.base.error;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

/**
 * @ClassName: McpErrorConfig
 * @Author: Liyy
 * @Description: 用户处理HTTP请求异常的拦击
 * @Date: 2019-07-12 10:50:52
 **/
@Configuration
public class McpErrorConfig {

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer(){
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                // 处理401错误,没有被授权
                ErrorPage page401 = new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/unAuthorized");
                //处理404错误
                ErrorPage page404 = new ErrorPage(HttpStatus.NOT_FOUND, "/error/notFind");
                //请求方式不被允许
                ErrorPage page405 = new ErrorPage(HttpStatus.METHOD_NOT_ALLOWED ,"/error/methodNotAllowed");
                //请求超时
                ErrorPage page408 = new ErrorPage(HttpStatus.REQUEST_TIMEOUT ,"/error/requestTimeOut");
                //请求超时
                ErrorPage page413 = new ErrorPage(HttpStatus.PAYLOAD_TOO_LARGE ,"/error/payloadTooLarge");
                container.addErrorPages(page401,page404,page405,page408,page413);
            }
        };
    }
}
